#####################################################################
#
#                          C++ Assistant
#
# 
# Author : Guillaume Delhumeau <guillaume.delhumeau@gmail.com>
# License : GNU General Public License 3
#
#####################################################################

import gedit
import gtk
import gobject
import SearchFileInProjectDialog
import File
import Project
import Navigator

# Menu item example, insert a new item in the Tools menu
ui_str = """<ui>
	<menubar name="MenuBar">
		<menu name="ToolsMenu" action="Tools">
			<placeholder name="cplusplusassist_group">
				<menuitem name="menuItmSearch" action="cppassist-search"/>
				<menuitem name="menuItmSwitch" action="cppassist-switch-h-cpp"/>
			</placeholder>
		</menu>
	</menubar>
</ui>
"""

class GeditWindow:
	""" A gedit window"""

	def __init__(self, plugin, window):
		""" Constructor """
		self._window = window
		self._plugin = plugin
				
		# Insert menu items
		self._insert_menu()

	def desactivate(self):
		""" Desactivate (kind of destructor) """
		self._window = None
		self._plugin = None
		pass

	def update_ui(self):
		""" Method called when gedit wants to update the ui """
		pass
		
	def _insert_menu(self):
		""" Create menu entries for the plugin """
		manager = self._window.get_ui_manager()
		
		self._action_group = gtk.ActionGroup("C++AssistActionGroup")
		self._action_group.add_actions([("cppassist-search", None, _("Search in project"), "<Shift><Alt>o", None, self.on_search_in_project)])
		self._action_group.add_actions([("cppassist-switch-h-cpp", None, _("Switch to .h/.cpp"), "<Alt>b", None, self.on_switch_h_cpp)])

		
		manager.insert_action_group(self._action_group, -1)
		
		self._ui_id = manager.add_ui_from_string(ui_str)
		
	def on_search_in_project(self, action):
		""" Search in project event """
		project = Project.look_for_project(self._window.get_active_document())
		if project <> None:		
			dialog = SearchFileInProjectDialog.SearchFileDialog(self._window, project)
			dialog.show()
		else:
			mess = gtk.MessageDialog(self._window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "There is no project.")
			mess.run()
			mess.destroy()
			
	def on_switch_h_cpp(self, action):
		""" Switch .h/.cpp """
		project = Project.look_for_project(self._window.get_active_document())
		if project <> None:		
			Navigator.switch_h_cpp(self._window)
		else:
			mess = gtk.MessageDialog(self._window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "There is no project.")
			mess.run()
			mess.destroy()
			
